package com.codeyang.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeyang.dao.ProdEsDao;
import com.codeyang.es.ProdEs;
import com.codeyang.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述:
 *
 * @author CodeYang_Site
 * @version 2021/5/29 15:38
 */
@Api(tags = "搜索管理接口")
@RestController
public class SearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private ProdEsDao prodEsDao;

    /**
     * 根据标签id查询商品
     *
     * @param tagId
     * @param size
     * @param current 不是必须的
     * @return
     */
    @GetMapping("prod/prodListByTagId")
    @ApiOperation("根据标签id查询商品")
    public ResponseEntity<Page<ProdEs>> prodListByTagId(
            @RequestParam("tagId") Long tagId,
            @RequestParam("size") Integer size,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current) {
        Page<ProdEs> prodEsPage = searchService.getProdByTags(tagId, size, current);
        return ResponseEntity.ok(prodEsPage);
    }


    @GetMapping("search/searchProdPage")
    @ApiOperation("根据关键字查询商品同时根据策略排序")
    public ResponseEntity<Page<ProdEs>> getProdByTagId(@RequestParam(value = "prodName", required = false) String prodName,
                                                       @RequestParam("size") Integer size,
                                                       @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                       @RequestParam("sort") Integer sort) {
        Page<ProdEs> prodByTag = searchService.getProdByProdName(prodName, size, current, sort);
        return ResponseEntity.ok(prodByTag);
    }

    //----远程调用

    /**
     * 远程调用获取prodEs集合
     *
     * @param prodIds
     * @return
     */
    @PostMapping("search/getProdEsByIds")
    @ApiOperation("根据ids查询商品的集合")
    List<ProdEs> getProdEsByIds(@RequestBody List<Long> prodIds) {

        // List实现了 Itearbe 可以 强转
        return (List<ProdEs>) prodEsDao.findAllById(prodIds);
    }


    @GetMapping("prod/pageProd")
    @ApiOperation("根据分类id查询")
    public ResponseEntity<Page<ProdEs>> loadCateGoryIdInfo(@RequestParam("cateGoryId") Long cateGoryId) {
        //设置page 显示第一页 20条数据
        Page<ProdEs> page = searchService.getProdByCategoryId(cateGoryId, 20, 1);
        return ResponseEntity.ok(page);
    }
}
